<template>
	<view class="calendar-container">
		<!-- 年月选择器 -->
		<view class="month-selector">
			<view class="selector-box">
				<picker mode="date" fields="month" :value="currentDate" @change="onDateChange">
					<view class="picker-content">
						<text>{{ yearMonth }}</text>
						<uni-icons type="bottom" size="14" color="#666"></uni-icons>
					</view>
				</picker>
			</view>
		</view>

		<!-- 日历网格 -->
		<view class="calendar-grid">
			<!-- 星期标题 -->
			<view class="week-header">
				<view class="week-item" v-for="(week, index) in weekDays" :key="index">{{ week }}</view>
			</view>
			
			<!-- 日期网格 -->
			<view class="days-grid">
				<view 
					class="day-item" 
					v-for="(day, index) in daysInMonth" 
					:key="index"
					:class="{ 
						'empty': !day,
						'attended': day && isAttended(day)
					}"
				>
					<view class="day-content" v-if="day">
						<text class="day-number">{{ day }}</text>
						<text v-if="isAttended(day)" class="attendance-text">已出勤</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			const now = new Date()
			const year = now.getFullYear()
			const month = String(now.getMonth() + 1).padStart(2, '0')
			return {
				currentDate: `${year}-${month}`, // 默认当前年月
				weekDays: ['日', '一', '二', '三', '四', '五', '六'],
				attendedDays: [] // 存储已出勤的日期
			}
		},
		computed: {
			yearMonth() {
				const [year, month] = this.currentDate.split('-')
				return `${year}年${month}月`
			},
			daysInMonth() {
				const [year, month] = this.currentDate.split('-')
				const date = new Date(year, month, 0)
				const daysCount = date.getDate()
				const firstDay = new Date(year, month - 1, 1).getDay()
				
				// 创建日期数组，前面补充空值
				const days = Array(firstDay).fill(null)
				for (let i = 1; i <= daysCount; i++) {
					days.push(i)
				}
				
				// 确保总长度是7的倍数
				while (days.length % 7 !== 0) {
					days.push(null)
				}
				
				return days
			}
		},
		methods: {
			onDateChange(e) {
				this.currentDate = e.detail.value
				this.generateRandomAttendedDays()
			},
			isAttended(day) {
				return this.attendedDays.includes(day)
			},
			generateRandomAttendedDays() {
				const [year, month] = this.currentDate.split('-')
				const date = new Date(year, month, 0)
				const daysCount = date.getDate()
				
				// 生成1到当月天数之间的随机数
				const getRandomDay = () => Math.floor(Math.random() * daysCount) + 1
				
				// 生成5个不重复的随机日期
				const days = new Set()
				while (days.size < 5) {
					days.add(getRandomDay())
				}
				
				this.attendedDays = Array.from(days)
			}
		},
		created() {
			this.generateRandomAttendedDays()
		}
	}
</script>

<style lang="scss" scoped>
.calendar-container {
	padding: 20rpx;
	background-color: #f5f7fa;
	min-height: 100vh;
}

.month-selector {
	margin-bottom: 30rpx;
	
	.selector-box {
		background: #fff;
		border-radius: 12rpx;
		padding: 20rpx;
		box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.05);
		
		.picker-content {
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 32rpx;
			color: #333;
			
			text {
				margin-right: 10rpx;
			}
		}
	}
}

.calendar-grid {
	background: #fff;
	border-radius: 12rpx;
	padding: 20rpx;
	box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.05);
	
	.week-header {
		display: grid;
		grid-template-columns: repeat(7, 1fr);
		margin-bottom: 20rpx;
		
		.week-item {
			text-align: center;
			font-size: 28rpx;
			color: #909399;
			padding: 10rpx 0;
		}
	}
	
	.days-grid {
		display: grid;
		grid-template-columns: repeat(7, 1fr);
		gap: 10rpx;
		
		.day-item {
			aspect-ratio: 1;
			display: flex;
			align-items: center;
			justify-content: center;
			background: #f5f7fa;
			border-radius: 8rpx;
			font-size: 28rpx;
			color: #333;
			position: relative;
			
			&.empty {
				background: transparent;
			}
			
			&.attended {
				background: #409EFF;
				color: #fff;
				
				.attendance-text {
					color: #fff;
				}
			}
			
			&:active {
				background: #e6f7ff;
				
				&.attended {
					background: #66b1ff;
				}
			}
			
			.day-content {
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				
				.day-number {
					font-size: 32rpx;
					font-weight: bold;
					margin-bottom: 4rpx;
				}
				
				.attendance-text {
					font-size: 20rpx;
					color: #fac221;
				}
			}
		}
	}
}
</style>
